<?php
namespace app\libs;
/**
 * rss 生成类
 */
class RSS {

    private $lastBuildDate = '';

    private $language = 'zh-cn';
    private $generator = 'RSS Generator';
    private $webMaster = '';
    private $channel_title = '';
    private $channel_link = '';
    private $channel_description = '';
    private $ttl = '1440';

    /**
     * RSS 单条信息的数组
     */
    private $items = [];

    /**
     * 构造函数
     * @param string $webMaster  管理员 emial
     * @param string $title  RSS 频道名
     * @param string $link  RSS 频道链接
     * @param string $description  RSS 频道描述
     */
    public function __construct($webMaster, $title, $link, $description) {
        $this->webMaster = $webMaster;
        $this->channel_title = $title;
        $this->channel_link = $link;
        $this->channel_description = $description;

        $this->lastBuildDate = date('Y-m-d H:i:s', time());
    }

    /**
     * 添加 RSS 项
     * @param string $title  标题
     * @param string $link  链接
     * @param string $author  作者
     * @param string $pubDate  文章日期
     * @param string $description  摘要
     */
     function addItem($title, $link, $author, $pubDate, $description) {
        $this->items[] = [
            'title' => $title,
            'link' => $link,
            'author' => $author,
            'pubDate' => $pubDate,
            'description' => $description
        ];
     }

    /**
     * 生成 RSS
     */
    public function fetch() {
        $rss = '<?xml version="1.0" encoding="utf-8" ?>';
        $rss .= '<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">';
        $rss .= '<channel>';
        $rss .= "<title>{$this->channel_title}</title>";
        $rss .= "<link>{$this->channel_link}</link>";
        $rss .= "<description>{$this->channel_description}</description>";
        $rss .= "<language>{$this->language}</language>";
        $rss .= "<copyright>{$this->generator}</copyright>";
        $rss .= "<webMaster>{$this->webMaster}</webMaster>";
        $rss .= "<pubDate>{$this->lastBuildDate}</pubDate>";
        $rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>";
        $rss .= "<generator>{$this->generator}</generator>";
        $rss .= "<ttl>{$this->ttl}</ttl>";

        for ($i = 0, $len=count($this->items); $i < $len; $i++) {
            $rss .= '<item>';
            $rss .= "<title>{$this->items[$i]['title']}</title>";
            $rss .= "<link>{$this->items[$i]['link']}</link>";
            $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>";
            $rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>";
            $rss .= "<author>{$this->items[$i]['author']}</author>";
            $rss .= '</item>';
        }

        $rss .= '</channel></rss>';

        return $rss;
    }

    /**
     * 输出 RSS
     */
    public function show() {
        header('Content-Type: text/xml; charset=utf-8');
        echo $this->fetch();
        exit;
    }

}
